package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.anno.AutoFill;
import com.itheima.aop.OperationType;
import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.Emp;
import com.itheima.pojo.PageBean;
import com.itheima.pojo.PageSelect;
import com.itheima.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class EmpServiceImpl implements EmpService {
    @Autowired
    private EmpMapper empMapper;

//    @Override
//    public List<Emp> list() {
//        return empMapper.list();
//    }

    @Override
    public void delete(List<Integer> ids) {
        empMapper.delete(ids);
    }

    @AutoFill(OperationType.INSERT)
    @Override
    public void insert(Emp emp) {
        emp.setPassword("123456");

        empMapper.insert(emp);
    }

    @Override
    public PageBean listByPage(Integer page, Integer pageSize, String name, Short gender, LocalDate begin, LocalDate end) {
//        //1. 获取总记录数
//        long total = empMapper.total();
//        //2. 获取分页查询的总记录数表
//        Integer start = (page-1)*pageSize;
//        List<Emp> list = empMapper.page(start,pageSize);
//        //3. 封装PageBean对象
//        PageBean pageBean = new PageBean(total,list);
        PageHelper.startPage(page,pageSize);
        List<Emp> EmpList = empMapper.list(name,gender,begin,end);
        Page<Emp> p = (Page<Emp>) EmpList;
        PageBean pageBean = new PageBean(p.getTotal(), p.getResult());
        return pageBean;
    }

    @AutoFill(OperationType.UPDATE)
    @Override
    public void update(Emp emp) {
        Emp e = empMapper.getByid(emp.getId());
        emp.setPassword(e.getPassword());

        empMapper.update(emp);
    }

    @Override
    public Emp login(Emp emp) {
        return empMapper.login(emp);
    }


//    @Override
//    public List<Emp> listByPage(PageSelect page) {
////        if (page.getPage() == null){
////            page.setPage(1);
////        }
//        return empMapper.selectbyPage(page);
//    }
}
